package com.ruoyi.robot.util;

import cn.hutool.core.io.FileUtil;
import cn.hutool.core.util.IdUtil;
import cn.hutool.core.util.StrUtil;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;

public class FileUploadUtil {

    public static String upload(MultipartFile multipartFile,String fileUrl) {
        String originalFilename = multipartFile.getOriginalFilename();
        String type = FileUtil.extName(originalFilename);//获取文件类型

        String uuid = IdUtil.fastSimpleUUID();
        String fileUUID = uuid + StrUtil.DOT+type;

        File uploadFile = new File(fileUrl+fileUUID);

        File parentFile = uploadFile.getParentFile();
        if(!parentFile.exists()){
            parentFile.mkdir();//如果不存在父文件就直接创建
        }

        try {
            multipartFile.transferTo(uploadFile);
        } catch (IOException e) {
            e.printStackTrace();
        }

        // return AjaxResult.success("http://localhost:9876/api/files/download/?fileName=" + fileUUID);
        return fileUUID;
    }
}
